package com.atguigu.statistics.service.impl;

import com.atguigu.statistics.client.UcenterClient;
import com.atguigu.statistics.entity.Daily;
import com.atguigu.statistics.mapper.DailyMapper;
import com.atguigu.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-09
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {
    @Autowired
    UcenterClient ucenterClient;

    @Override
    public void createStatisticsByDay(String day) {
        // 获取统计信息
        Integer registerNum = (Integer) ucenterClient.registerCount(day).getData().get("countRegister");
        Integer loginNum = 100;
        Integer videoViewNum = 100;
        Integer courseNum = 100;
        // 判断是否已经存在
        Daily daily = baseMapper.existByDateCalculate(day);
        if (daily != null) {
            daily.setRegisterNum(registerNum);
            daily.setLoginNum(loginNum);
            daily.setVideoViewNum(videoViewNum);
            daily.setCourseNum(courseNum);
            daily.setDateCalculated(day);
            // 更新
            baseMapper.updateById(daily);
        } else {
            //创建统计对象
            daily = new Daily();
            daily.setRegisterNum(registerNum);
            daily.setLoginNum(loginNum);
            daily.setVideoViewNum(videoViewNum);
            daily.setCourseNum(courseNum);
            daily.setDateCalculated(day);
            // 插入
            baseMapper.insert(daily);
        }
    }

    @Override
    public Daily existByDateCalculate(String day) {
        return baseMapper.existByDateCalculate(day);
    }

    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {

        QueryWrapper<Daily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.select(type, "date_calculated");
        dayQueryWrapper.between("date_calculated", begin, end);

        List<Daily> dayList = baseMapper.selectList(dayQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<Integer>();
        List<String> dateList = new ArrayList<String>();
        map.put("dataList", dataList);
        map.put("dateList", dateList);

        for (int i = 0; i < dayList.size(); i++) {
            Daily daily = dayList.get(i);
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        return map;
    }


}
